// 主题切换服务
class ThemeService {
  constructor() {
    this.currentTheme = 'default'
    this.themes = new Map()
    this.isInitialized = false
  }

  /**
   * 初始化主题服务
   */
  initialize() {
    if (this.isInitialized) return

    // 注册默认主题
    this.registerTheme('default', {
      primary: '#42b883',
      secondary: '#35495e',
      background: '#ffffff',
      surface: '#f5f5f5',
      text: '#333333',
      'on-primary': '#ffffff',
      'on-secondary': '#ffffff',
      'on-background': '#333333',
      'on-surface': '#333333'
    })

    // 注册AI起名主题
    this.registerTheme('ai-naming', {
      primary: '#6a11cb',
      secondary: '#2575fc',
      background: 'linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%)',
      surface: '#ffffff',
      text: '#2d3748',
      'on-primary': '#ffffff',
      'on-secondary': '#ffffff',
      'on-background': '#2d3748',
      'on-surface': '#4a5568'
    })

    // 注册Office PDF主题
    this.registerTheme('office-pdf', {
      primary: '#1e3a8a',
      secondary: '#3b82f6',
      background: '#f8fafc',
      surface: '#ffffff',
      text: '#1f2937',
      'on-primary': '#ffffff',
      'on-secondary': '#ffffff',
      'on-background': '#1f2937',
      'on-surface': '#374151'
    })

    // 注册AI机器人主题
    this.registerTheme('ai-bot', {
      primary: '#ec4899',
      secondary: '#8b5cf6',
      background: 'linear-gradient(135deg, #1e1b4b 0%, #3730a3 100%)',
      surface: '#3730a3',
      text: '#f3e8ff',
      'on-primary': '#ffffff',
      'on-secondary': '#ffffff',
      'on-background': '#f3e8ff',
      'on-surface': '#e9d5ff'
    })

    // 从localStorage恢复主题
    const savedTheme = localStorage.getItem('current-theme')
    if (savedTheme && this.themes.has(savedTheme)) {
      this.applyTheme(savedTheme, false) // 不保存到localStorage
    }

    this.isInitialized = true
    console.log('ThemeService initialized')
  }

  /**
   * 注册主题
   * @param {string} name - 主题名称
   * @param {Object} variables - CSS变量
   */
  registerTheme(name, variables) {
    this.themes.set(name, variables)
  }

  /**
   * 应用主题
   * @param {string} themeName - 主题名称
   * @param {boolean} save - 是否保存到localStorage
   */
  applyTheme(themeName, save = true) {
    const theme = this.themes.get(themeName)

    if (!theme) {
      console.warn(`Theme '${themeName}' not found`)
      return false
    }

    // 移除现有主题类
    document.body.className = document.body.className.replace(/theme-\S+/g, '')

    // 添加新主题类
    document.body.classList.add(`theme-${themeName}`)

    // 更新CSS变量
    Object.keys(theme).forEach(key => {
      document.documentElement.style.setProperty(`--${key}`, theme[key])
    })

    this.currentTheme = themeName

    // 保存到localStorage
    if (save) {
      localStorage.setItem('current-theme', themeName)
    }

    console.log(`Theme '${themeName}' applied`)
    return true
  }

  /**
   * 切换到下一个主题
   */
  cycleTheme() {
    const themeNames = Array.from(this.themes.keys())
    const currentIndex = themeNames.indexOf(this.currentTheme)
    const nextIndex = (currentIndex + 1) % themeNames.length
    const nextTheme = themeNames[nextIndex]

    return this.applyTheme(nextTheme)
  }

  /**
   * 获取所有可用主题
   */
  getThemes() {
    return Array.from(this.themes.keys())
  }

  /**
   * 获取主题配置
   * @param {string} themeName - 主题名称
   */
  getThemeConfig(themeName) {
    return this.themes.get(themeName) || null
  }

  /**
   * 检查主题是否存在
   * @param {string} themeName - 主题名称
   */
  hasTheme(themeName) {
    return this.themes.has(themeName)
  }

  /**
   * 获取当前主题名称
   */
  getCurrentTheme() {
    return this.currentTheme
  }

  /**
   * 重置所有主题样式
   */
  reset() {
    document.body.className = document.body.className.replace(/theme-\S+/g, '')
    document.documentElement.style.cssText = ''
    this.currentTheme = 'default'

    // 重新应用默认主题
    this.applyTheme('default', false)
  }
}

// 创建全局实例
const themeService = new ThemeService()

export default themeService
export { ThemeService }